package com.digitalconsulting.phototraveler;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.digitalconsulting.phototraveler.datalayer.ItemListDataSource;
import com.zooz.android.lib.CheckoutActivity;

public class MainActivity extends Activity {

	private Button btnAddItem;
	private Button btnShoppingList;

	private static final int ZooZ_Activity_ID = 1;
	private static final String TAG = MainActivity.class.getSimpleName();

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			Intent locationSettingsIntent = new Intent(getApplicationContext(),
					SettingsActivity.class);
			startActivity(locationSettingsIntent);
			return true;

		case R.id.about:
			final Dialog dialog = new Dialog(MainActivity.this);
			dialog.setContentView(R.layout.about_dialog);
			dialog.setTitle("About");

			//PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
			//version = pInfo.versionName;
			
			Button button = (Button) dialog.findViewById(R.id.btnClose);
			button.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					dialog.dismiss();
				}
			});

			dialog.show();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == ZooZ_Activity_ID) {

			
			switch (resultCode) {
			case Activity.RESULT_OK:
				Log.i(TAG,
						"Succefuly paid. Your transaction id is: "
								+ data.getStringExtra(CheckoutActivity.ZOOZ_TRX_ID));
				
				File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES) , "key");
				try
				{
				file.createNewFile();
				}
				catch (IOException ex) 
				{
					Log.i(TAG, ex.toString());
				}
				
				break;
			case Activity.RESULT_CANCELED:

				if (data != null)
					Log.e(TAG,
							"Error, cannot complete payment with ZooZ. "
									+ "Error code: "
									+ data.getIntExtra(
											CheckoutActivity.ZOOZ_ERROR_CODE, 0)
									+ "; Error Message: "
									+ data.getStringExtra(CheckoutActivity.ZOOZ_ERROR_MSG));
				break;

			default:
				break;
			}
			
		}

	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);


		
		
		Typeface font = Typeface.createFromAsset(getAssets(), "CURLZ___.TTF");

		btnAddItem = (Button) findViewById(R.id.btnAddItem);
		btnAddItem.setTypeface(font);
		btnAddItem.setBackgroundColor(Color.WHITE);

		btnShoppingList = (Button) findViewById(R.id.btnShoppingList);
		btnShoppingList.setTypeface(font);
		btnShoppingList.setBackgroundColor(Color.WHITE);

		btnAddItem.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				ItemListDataSource ds = new ItemListDataSource(
						getApplicationContext());
				
				File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES) , "key");
				
				
				if ((ds.getTotalRows() >= 3) && (!file.exists())) {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							MainActivity.this);
					builder.setMessage(
							"Free version is limited to maximum 3 items!")
							.setCancelable(false)
							.setPositiveButton(R.string.buy,
									new DialogInterface.OnClickListener() {

										public void onClick(
												DialogInterface dialog,
												int which) {
											Intent intent = new Intent(MainActivity.this, CheckoutActivity.class);
											intent.putExtra(CheckoutActivity.APP_KEY, "75eab31e-a985-48db-8105-03293eb4d626");
											intent.putExtra(CheckoutActivity.PAY_AMOUNT, 0.99);
											intent.putExtra(CheckoutActivity.CURRENCY_CODE, "EUR");
											intent.putExtra(CheckoutActivity.IS_SANDBOX, false);
											startActivityForResult(intent, ZooZ_Activity_ID);
											
											dialog.cancel();

										}
									}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

										public void onClick(
												DialogInterface dialog,
												int which) {
											dialog.cancel();}}
										).show();

				} else {
					Intent intent = new Intent();
					intent.setClass(getApplicationContext(),
							AddItemActivity.class);
					startActivity(intent);
				}

			}
		});

		btnShoppingList.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				ItemListDataSource ds = new ItemListDataSource(
						getApplicationContext());
				if (ds.getTotalRows() == 0) {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							MainActivity.this);
					builder.setMessage("Shopping list is empty.")
							.setCancelable(false)
							.setPositiveButton("OK",
									new DialogInterface.OnClickListener() {

										public void onClick(
												DialogInterface dialog,
												int which) {
											dialog.cancel();

										}
									}).show();

				} else {
					Intent intent = new Intent();
					intent.setClass(getApplicationContext(),
							PhotoTravelerGalleryActivity.class);
					intent.putExtra("sort", "NONE");
					startActivity(intent);
				}
				/*
				 * Intent intent = new Intent();
				 * intent.setClass(getApplicationContext(),
				 * ShoppingListActivity.class); intent.putExtra("sort", "NONE");
				 * startActivity(intent);
				 */
			}
		});
	}
}